<script setup lang="ts">
  import { ref, computed, watch, reactive, onMounted } from 'vue';
  import { useBooleanStore } from '@/store';
  import { outInFlowSearch } from '@/api/inventory-management';
  import { OutInFlowParam } from '@/api/inventory-management/types';
  import { Notification } from '@arco-design/web-vue';

  const booleanStore = useBooleanStore();
  const emit = defineEmits(['updateArray']);
  const pageSize = ref<number>(10);
  const pageNumber = ref<number>(1);
  const rangeDate = ref<Date[]>([]);
  const formValues = reactive({
    itemCode: '',
    qualityCode: '',
    aoNo: '',
    hgzNo: '',
    outInStatus: '',
    invTransactStatus: '',
    startTime: '',
    endTime: '',
    wmsItemInventoryId: '',
    allotId: '',
  });
  watch(rangeDate, () => {
    if (rangeDate.value && rangeDate.value.length >= 2) {
      formValues.startTime = rangeDate.value[0].toString();
      formValues.endTime = rangeDate.value[1].toString();
    }
  });
  const outInStatusSend = computed(() => {
    if (formValues.outInStatus === '') {
      return '';
    }
    if (formValues.outInStatus === '入库') {
      return 'IN';
    }
    return 'OUT';
  });
  const invTransactStatusSend = computed(() => {
    if (formValues.invTransactStatus === '') {
      return '';
    }
    if (formValues.invTransactStatus === '正常') {
      return '1';
    }
    return '4';
  });
  // 发送搜索数据
  const flowQueryData = computed<OutInFlowParam>(() => ({
    queryParam: {
      itemCode: formValues.itemCode,
      qualityCode: formValues.qualityCode,
      aoNo: formValues.aoNo,
      hgzNo: formValues.hgzNo,
      outInStatus: outInStatusSend.value,
      invTransactStatus: invTransactStatusSend.value,
      startTime: formValues.startTime,
      endTime: formValues.endTime,
      wmsItemInventoryId: formValues.wmsItemInventoryId,
      allotId: formValues.allotId,
    },
    page: {
      total: 0,
      pageSize: pageSize.value,
      currentPage: pageNumber.value,
    },
  }));
  async function handleSearch() {
    const newData = await outInFlowSearch(flowQueryData.value);
    if (newData) {
      emit('updateArray', newData);
      if (newData.list.length === 0) {
        Notification.success({
          title: '成功',
          content: '当前查询条件下无数据',
          closable: true,
        });
      }
    } else {
      Notification.error({
        title: '失败',
        content: newData.message ? newData.message : '查询失败',
        closable: true,
      });
    }
  }
  function reset() {
    Object.keys(formValues).forEach((key) => {
      (formValues as Record<string, any>)[key] = '';
    });
    rangeDate.value = [];
    setTimeout(() => {
      handleSearch();
    }, 150);
  }
  // 监听分页
  watch([() => booleanStore.pageNum, () => booleanStore.pageSize], () => {
    if (booleanStore.num5 === 35) {
      pageNumber.value = booleanStore.pageNum;
      pageSize.value = booleanStore.pageSize;
      handleSearch();
    }
  });
  onMounted(() => {
    handleSearch();
  });
</script>

<template>
  <a-row :gutter="{ md: 8, lg: 24, xl: 32 }" class="input-col">
    <a-col :span="6">
      <a-input
        v-model="formValues.itemCode"
        placeholder="图号"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.qualityCode"
        placeholder="质量编号"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.aoNo"
        placeholder="AO编号"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.hgzNo"
        placeholder="合格证号"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.wmsItemInventoryId"
        placeholder="台账ID"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.allotId"
        placeholder="占用ID"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-select
        v-model="formValues.outInStatus"
        placeholder="出入库类型"
        @press-enter="handleSearch"
      >
        <a-option>出库</a-option>
        <a-option>入库</a-option>
      </a-select>
    </a-col>
    <a-col :span="6">
      <a-select
        v-model="formValues.invTransactStatus"
        placeholder="流水状态"
        @press-enter="handleSearch"
      >
        <a-option>撤销</a-option>
        <a-option>正常</a-option>
      </a-select>
    </a-col>
    <a-col :span="12">
      <a-range-picker v-model:model-value="rangeDate" />
    </a-col>
  </a-row>
  <div style="margin-top: 16px">
    <a-button type="primary" size="large" @click="handleSearch">查询</a-button>
    <a-button class="btn-reset" size="large" @click="reset">重置</a-button>
  </div>
</template>

<style scoped>
  span {
    margin-bottom: 16px;
  }
  .btn-reset {
    margin-left: 10px;
  }
</style>
